/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.base.web;

import com.farmcode.base.dao.BaseFilter;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author oiprado
 */
public abstract class BaseMasterDetailBacking<E> extends BaseBacking {

  private E entity;
  private E create;
  private List<E> entities;
  
  

  public BaseMasterDetailBacking() {
    entities = new ArrayList<E>();
  }

  public BaseMasterDetailBacking(E entity) {
    this();
  }

  public void showPopupMessage(String message){
    
  }
  
  
  public void onFind(){
    find();
  }
  
  public void onCreate(){
    create();
    showPopupMessage("Created");
    find();
  }

  public void onStore(E entity) {
    try {
      store(entity);
      E lastRowSelected = getEntity();

      if (getEntity() != null) {
        ((RowSelected) lastRowSelected).setSelected(false);
      }
      setEntity(entity);
      if (entity instanceof RowSelected) {
        ((RowSelected) getEntity()).setSelected(true);
      }
      showPopupMessage("Saved");
      find();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void onCancel(E entity) {
    E lastRowSelected = getEntity();

    if (getEntity() != null) {
      ((RowSelected) lastRowSelected).setSelected(false);
    }
    setEntity(null);
    
    find();
  }

  public void onRemove(E entity) {
    remove(entity);
    showPopupMessage("Removed");
    find();
  }

  public void onSelectRow(E entity) {
    E lastRowSelected = getEntity();

    if (getEntity() != null) {
      ((RowSelected) lastRowSelected).setSelected(false);
    }
    setEntity(entity);
    if (entity instanceof RowSelected) {
      ((RowSelected) getEntity()).setSelected(true);
    }
  }

  protected abstract void create();
  
  protected abstract void store(E entity);
  
  protected abstract void remove(E entity);

  protected abstract void find();

  /**
   * @return the entity
   */
  public E getEntity() {
    if (getSession().getValue(this.getClass().getCanonicalName().toLowerCase() + "_entity") != null) {
      entity = (E) getSession().getValue(this.getClass().getCanonicalName().toLowerCase() + "_entity");
    }
    return entity;
  }

  /**
   * @param entity the entity to set
   */
  public void setEntity(E entity) {
    this.entity = entity;
    getSession().setValue(this.getClass().getCanonicalName().toLowerCase() + "_entity", entity);
  }

  /**
   * @return the entities
   */
  public List<E> getEntities() {
    if (getSession().getValue(this.getClass().getCanonicalName().toLowerCase() + "_list") == null) {
      find();
    } else {
      entities = (List<E>) getSession().getValue(this.getClass().getCanonicalName().toLowerCase() + "_list");
    }

    return entities;
  }

  /**
   * @param entities the entities to set
   */
  public void setEntities(List<E> entities) {
    this.entities = entities;
    getSession().setValue(this.getClass().getCanonicalName().toLowerCase() + "_list", entities);
  }

  /**
   * @return the create
   */
  public E getCreate() {
    if (getSession().getValue(this.getClass().getCanonicalName().toLowerCase() + "_entity_new") != null) {
      create = (E) getSession().getValue(this.getClass().getCanonicalName().toLowerCase() + "_entity_new");
    }
    return create;
  }

  /**
   * @param create the create to set
   */
  public void setCreate(E create) {
    this.create = create;
    getSession().setValue(this.getClass().getCanonicalName().toLowerCase() + "_entity_new", create);
  }

//  @Override
//  public void beforeCreate() {
//    throw new UnsupportedOperationException("Not supported yet.");
//  }
//
//  @Override
//  public void afterCreate() {
//    throw new UnsupportedOperationException("Not supported yet.");
//  }

  /**
   * @return the baseFilter
   */
  public abstract BaseFilter getBaseFilter();

  /**
   * @param baseFilter the baseFilter to set
   */
  public abstract void setBaseFilter(BaseFilter baseFilter);
}
